<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\index\controller;
use app\common\controller\Fornt;
use app\common\model\Cart;
use app\common\model\Goodscat;
use app\common\model\Address;
use app\common\model\DeliveryArea;
use app\common\model\DeliveryTime;
use app\common\model\Document;
use think\Db;

/**
 * 用户页面
 */
class User extends Fornt{

  public function index(){
    update_session(USER_ID);
    return $this->fetch();
  }

  public function qiye($cid = null){
    $info = parent::setQiyeCommon();
    return $this->fetch();
  }



  public function help(){
    $list = Document::where(['category_id'=>8])->order('level asc,id asc')->select();
    $this->assign('list', $list);
    return $this->fetch();
  }
  public function info(){
    if (IS_POST) {
      $data['name'] = input('post.name');
      $data['phone'] = input('post.phone');
      $data['birthday'] = input('post.birthday');
      $data['birthday_monthday'] = input('post.birthday_monthday');

      //判断是否是第一次提交
      $info = Db::name('wxUser')->where('id', USER_ID)->find();
      if(empty($info['name']) && empty($info['phone'])){
        sendcomSms('SMS_68690029', ['user'=>$data['name'], 'balance'=>(string)$info['balance'], 'integral'=>(string)$info['integral']], (string)$data['phone']);
      }
      //注册时间
      if(!$info['member_time']){
        $data['member_time'] = time();
      }
      $return = Db::name('wxUser')->where('id', USER_ID)->update($data);


      //如果是企业开启会员则添加默认用户
      if(cookie('company_id')){
        //设置所有地址为不默认
				$map = ['uid'=>USER_ID,'company_id'=>cookie('company_id')];
				model('Qiyeuser')->where($map)->update(['defaults' => 0]);
			 //添加用户信息

			 $dataq['username'] = input('post.name');
			 $dataq['phone'] = input('post.phone');
			 $dataq['defaults'] = 1;
			 $dataq['uid'] = USER_ID;
			 $dataq['company_id'] = cookie('company_id');
			 $result = model('Qiyeuser')->changes($dataq);
      }

      if($return){
        update_session(USER_ID);
        $this->success('注册成功');
      }else{
        $this->error('修改失败');
      }

    }else{
      $info = Db::name('wxUser')->find(USER_ID);
      $this->assign('info', $info);
      return $this->fetch();
    }
  }

}
